/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package justdoit.view;

import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import justdoit.view.model.TasksTableModel;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.TableColumnModel;
import javax.swing.table.TableRowSorter;
import justdoit.Startup;
import justdoit.actions.DeleteTaskAction;
import justdoit.actions.EditTaskAction;
import justdoit.actions.MarkAsDoneAction;
import justdoit.model.Task;
import justdoit.model.User;

/**
 * The table to display the tasks
 * @author Can
 */
public class TaskTable extends JTable {

    private TableColumnModel tcm;
    private User user;
    private JPopupMenu popMenu;
    private TableRowSorter sorter;

    public TaskTable(TasksTableModel tasksTableModel, User user) {
        super(tasksTableModel);
        this.user = user;

        tcm = this.getColumnModel();

        tcm.getColumn(0).setPreferredWidth(3);      //Pri.
        tcm.getColumn(1).setPreferredWidth(300);    //Task Description
        tcm.getColumn(2).setPreferredWidth(100);    //Progress
        tcm.getColumn(3).setPreferredWidth(20);     //Category
        tcm.getColumn(4).setPreferredWidth(20);     //Deadline

        tcm.getColumn(0).setCellRenderer(new TasksTableCellRenderer());
        //tcm.getColumn(2).setCellRenderer(new TasksTableCellRenderer());
        tcm.getColumn(2).setCellRenderer(new ProgressBarRenderer());
        tcm.getColumn(2).setCellEditor(new ProgressBarEditor());

        popMenu = new JPopupMenu();
        popMenu.add(new JMenuItem(new EditTaskAction()));
        popMenu.add(new JMenuItem(new DeleteTaskAction()));
        popMenu.addSeparator();
        popMenu.add(new JMenuItem(new MarkAsDoneAction()));

        this.setName("table");
        this.setRowSelectionAllowed(true);
        this.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        // sort
        this.sorter = new TableRowSorter(tasksTableModel);
        this.sorter.setSortsOnUpdates(false);
        for (int i = 0; i < tcm.getColumnCount(); i++) {
            sorter.setComparator(i, new ComparatorForTable(true));
        }
        this.setRowSorter(sorter);

        this.getSelectionModel().addListSelectionListener(new ListSelectionListener() {

            public void valueChanged(ListSelectionEvent e) {
                notifySelectionChanged();
            }
        });

        this.addMouseListener(new MouseAdapter() {

            @Override
            public void mouseClicked(MouseEvent e) {
                // double clicks by left button
                if (e.getComponent().isEnabled() && e.getButton() == MouseEvent.BUTTON1 && e.getClickCount() == 2) {
                    Point p = e.getPoint();
                    int rowIndex = rowAtPoint(p);
                    if (rowIndex != -1) {
                        Task task = ((TasksTableModel) getModel()).getTask(rowIndex);
                        if (task == null) {
                            System.out.println("Should not happen!");
                            return;
                        }
                        EditTaskDialog etd = new EditTaskDialog(task);
                        etd.setVisible(true);
                    }
                } else if (e.getButton() == MouseEvent.BUTTON3) {
                    // right click
                    popMenu.show(e.getComponent(), e.getX(), e.getY());
                    popMenu.setVisible(true);
                } else {
                    popMenu.setVisible(false);
                }
            }
        });

        this.setFocusable(false);

        if (this.getSelectedRowCount() <= 0 && this.getRowCount() > 0) {
            this.changeSelection(0, 0, false, false);
        }
    }

    public void refresh(List<Task> tasks) {
        ((TasksTableModel) this.getModel()).setTasks(tasks);
        if (this.getRowCount() > 0) {
            this.sorter.setModel(this.getModel());
        }
        this.updateUI();
    }

    private void notifySelectionChanged() {
        MainFrame mf = Startup.getMainFrame();
        // mf.getTaskDetails(this.getSelectedTask());
    }
}
